/**
 * Created by cc
 * Description:
 * User: CZH
 * Date: 2022-09-18
 * Time: 21:25
 */
class ListNodeD {
    public int data;    //数据域
    public ListNodeD prev;  //前驱
    public ListNodeD next;  //后续

    public ListNodeD(int data) {
        this.data = data;
    }
}

public class MyRealLinkedList {
    //头尾节点事关全局，不建议改变位置
    public ListNodeD head;  //头
    public ListNodeD last;  //尾

    //输出链表数据
    public void display(){
        if (this.head == null) {
            System.out.println("链表为空");
            return;
        }
        ListNodeD cur = this.head;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //获取链表长度
    public int size(){
        int count = 0;
        if (this.head == null) {
            return count;
        }
        ListNodeD cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //头插法
    public void addFirst(int data) {
        ListNodeD node = new ListNodeD(data);
        if (this.head == null) {
            this.head = node;
            this.last = node;
        }else {
            node.next = this.head;
            this.head.prev = node;
            this.head = node;
        }
    }

    //尾插法
    public void addLast(int data) {
        ListNodeD node = new ListNodeD(data);
        if (this.head == null) {
            this.last = node;
            this.head = node;
        }else {
            this.last.next = node;
            node.prev = this.last;
            this.last = node;
        }
    }


    //找index位置的前一位节点
    public ListNodeD findIndex(int index) {
        int count = 0;
        ListNodeD cur = this.head;
        while (count < index-1) {
            count++;
            cur = cur.next;
        }
        return cur;
    }

    //任意位置插入，第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        //判断index是否合法
        if (index < 0 || index > size()) {
            System.out.println("index有问题");
            return;
        }
        //判断链表是否有数据
        if (this.head == null) {
            System.out.println("链表数据为空");
            return;
        }
        //头插法
        if (index == 0) {
            addFirst(data);
            return;
        }
        //尾插法
        if (index == size()) {
            addLast(data);
            return;
        }
        //插入链表中间位置
        ListNodeD cur = findIndex(index);
        ListNodeD node = new ListNodeD(data);
        node.prev = cur;
        node.next = cur.next;
        cur.next.prev = node;
        cur.next = node;
    }

    //查找是否包含关键字key.是否在单链表当中
    public boolean contains(int key) {
        //链表为空
        if (this.head == null) {
            return false;
        }
        ListNodeD cur = this.head;
        while (cur != null){
            if (key == cur.data) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        //判断空表
        if (this.head == null) {
            System.out.println("空表");
            return;
        }
        //判断是否有key在链表中
        if (!contains(key)) {
            System.out.println("key不存在");
            return;
        }
        ListNodeD cur = this.head;
        //删除节点在头节点
        if (this.head.data == key) {
            this.head = this.head.next;
            this.head.prev = null;
            cur.next = null;
            return;
        }
        //删除节点在尾节点
        if (this.last.data == key) {
            this.last = this.last.prev;
            this.last.next = null;
            cur.prev = null;
            return;
        }
        //删除节点在中间
        cur.prev.next = cur.next;
        cur.next.prev = cur.prev;
    }







}
